package com.newsproject.service.impl;

import Util.PageInfo;
import com.newsproject.mapper.AdminMapper;
import com.newsproject.pojo.*;
import com.newsproject.pojo.DTO.*;
import com.newsproject.pojo.vo.BusinessException;
import com.newsproject.service.AdminService;
import com.newsproject.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;

/*业务层*/
@Service
public class AdminServiceImpl implements AdminService {
@Autowired
    private AdminMapper adminMapper;
@Autowired
    private PasswordEncoder passwordEncoder;
@Autowired
    private EmailService emailService;
/* 管理员对用户的管理 */

/*调用Mapper层方法：通过注入的 adminMapper 对象调用其 getUserList 方法
    参数传递：将业务层接收到的分页参数 start(起始偏移量) 和 pageSize(每页大小) 直接传递给Mapper层
    返回结果：将Mapper层查询到的用户列表 List<User> 直接返回给调用方*/

//获取用户列表(分页查询)
@Override
@Transactional
public UserListResponseDTO getUserList(int currentPage, int pageSize, String keyword){
     //1.参数校验
     if(currentPage<0 || pageSize<=0){
         Result.error("分页参数无效!");
     }
     int start = (currentPage - 1) * pageSize;  //计算起始偏移量
     List<UserDTO> users;     //用户列表
     int totalCount;          //总记录数
     int totalPages;          //总页数
     //根据关键字搜索用户列表实现逻辑
     if(keyword != null && !keyword.isEmpty()){
         //执行搜索逻辑
         users = adminMapper.searchUsers(keyword,start,pageSize);
         totalCount = adminMapper.getUsersCountByKeyword(keyword);
         totalPages = (int) Math.ceil((double) totalCount / pageSize);
     } else {
         //执行用户正常展示逻辑
         users = adminMapper.getUserList(start,pageSize);
         totalCount = adminMapper.getAllUserCount();
         totalPages = (int) Math.ceil((double) totalCount / pageSize);
     }
     //将封装后的数据返回到Controller层
     return new UserListResponseDTO(
        users,
        new PageInfo(totalCount,pageSize,currentPage),
        totalPages
     );
}

//获取用户总数(用于分页)
@Override
    public int getAllUserCount(){
    return adminMapper.getAllUserCount();
}

//根据关键词获取用户总数(用于分页)
@Override
    public int getUsersCountByKeyword(String keyword){
       return adminMapper.getUsersCountByKeyword(keyword);
    }

//根据userId获取用户信息
@Override
    public User getUserById(int userId) {
    //1.参数校验
    if (userId <= 0) {
        Result.error("用户ID无效!");
    }
    //2.检查用户是否存在
    User user = adminMapper.getUserById(userId);
    if (user == null) {
        Result.error("用户不存在!");
    }
    return user;
}

//2.添加用户
@Transactional
@Override
    public void addUser(User user) {
        //1.参数校验
        if(user == null || StringUtils.isEmpty(user.getUserName())){
            Result.error("用户名不能为空!");
        }
        //2.检查用户名是否已存在
        User existingUser = adminMapper.getUserByName(user.getUserName());
        if(existingUser != null){
            Result.error("用户名已存在!");
        }
        //3.添加用户并初始化用户状态
        UserStatus userStatus = new UserStatus();
        adminMapper.addUser(user);
        adminMapper.addUserStatus(userStatus);
        Result.success(user, "用户添加成功!");

    //生成带id的初始密码
    String rawPassword = "FutureNews" + user.getUserId();
    //使用加密器加密
    String encodedPassword = passwordEncoder.encode(rawPassword);
    //更新用户表中的密码
    adminMapper.updateUserPassword(user.getUserId(), encodedPassword);

    //发送邮件通知用户
    String subject = "欢迎加入FutureNews社区";
    String content = "您的初始密码为：" + rawPassword + "\n请及时修改密码以保证账户安全。";

    //调用邮箱发送方法
    emailService.sendEmail(user.getEmail(), subject, content);
}

//用户编辑
@Override
@Transactional
    public void updateUser(User user) {
      // 1. 参数校验
      if (user == null || user.getUserId() <= 0) {
          throw new BusinessException(500,"用户ID无效!");
      }
      // 2. 检查用户是否存在
      User existingUser = adminMapper.getUserById(user.getUserId());
      if (existingUser == null) {
         throw new BusinessException(500,"用户不存在!");
      }
      //检查用户名是否已存在
    String newUserName  = user.getUserName();
    if(!existingUser.getUserName().equals(newUserName)) {
        User duplicateUser = adminMapper.getUserByName(newUserName);
        if (duplicateUser != null) {
            throw new BusinessException(500,"用户名已被使用，请更换其他用户名！");
        }
    }
      //3.更新用户信息
      adminMapper.updateUser(user);
      Result.success(user, "用户信息更新成功!");
}

//通过id删除用户
@Override
@Transactional
    public Result<?> deleteUserById(int userId) {
   try {
       // 1. 参数校验-检查userId是否有效
       if (userId <= 0) {
           Result.error("用户ID无效");
       }
       // 2. 检查用户是否存在
       User user = adminMapper.getUserById(userId);
       if (user == null) {
           Result.error("用户不存在");
       }
       //3.删除用户
       adminMapper.deleteUserById(userId);
       return Result.success("用户删除成功!");
   } catch (Exception e) {
       return Result.error("删除失败");
   }
}

//批量删除多个用户
@Override
    public void deleteUserByIds(int[] userIds) {
  try {
      //1.参数校验
      if (userIds == null || userIds.length == 0) {
          Result.error("用户ID列表不能为空!");
      }
      //2.检查用户是否存在
      for (int userId : userIds) {
          if (userId <= 0) {
              Result.error("用户ID无效!");
          }
          User user = adminMapper.getUserById(userId);
          if (user == null) {
              Result.error("用户ID " + userId + " 不存在!");
          }
      }
      //3.批量删除
      adminMapper.deleteUserByIds(userIds);
      Result.success("用户批量删除成功!");
  } catch (Exception e) {
      Result.error("批量删除失败!");
  }
}

/*//模糊查询用户信息
@Override
    public List<UserDTO> searchUsers(String keyword, int start, int pageSize) {
    //1.参数校验
   if(StringUtils.isEmpty(keyword)) {
        Result.error("查询关键字不能为空!");
    }
    if(start<0 || pageSize<=0){
        Result.error("分页参数无效!");
    }
    //2.执行方法
    return adminMapper.searchUsers(keyword,start,pageSize);
}*/

//设置禁言管理
@Transactional
@Override
    public void banUser(int userId, boolean isBanned) {
       UserStatus userStatus =  adminMapper.getUserStatusById(userId);
       System.out.println(getUserById(userId));
       if (userStatus != null) {
           userStatus.setIsBanned(isBanned ? 1: 0);
           adminMapper.updateUserBanStatus(userId,isBanned);
       }else {
           Result.error("用户状态不存在!");
       }
}

//设置私信权限
@Override
    public void setPrivateMessagePermission(int userId, boolean isPrivateMessage) {
    UserStatus userStatus =  adminMapper.getUserStatusById(userId);
    if (userStatus != null) {
        userStatus.setIsPrivateMessage(isPrivateMessage ? 1 : 0);
        adminMapper.updatePrivateMessagePermission(userId,isPrivateMessage);
    } else {
        Result.error("用户状态不存在!");
    }
}

//设置账号状态
@Override
   public void setBanAccountPermission(int userId, boolean canBanAccount){
    UserStatus userStatus =  adminMapper.getUserStatusById(userId);
    if (userStatus != null) {
        userStatus.setBanAccount(canBanAccount ? 1 : 0);
        adminMapper.updateAccountStatus(userId,canBanAccount);
    } else {
        Result.error("用户状态不存在!");
    }
}

//批量设置用户权限
@Transactional
@Override
    public void batchUpdateUserPermissions(List<UserStatus> updateList){
  try{
    //1.参数校验
    if(updateList == null || updateList.size() <= 0) {
         Result.error("用户更新列表不能为空!");
     }
    //2.批量更新用户状态
     for(UserStatus userStatus : updateList) {
         if(userStatus.getUserStatusId() <= 0){
             Result.error("用户ID有误！");
         }
         adminMapper.batchUpdateUserPermissions(updateList);
         Result.success(updateList,"用户权限批量更新成功!");
     }
  } catch (Exception e) {
      Result.error("批量更新失败!");
  }
}


/* 管理员对热搜的管理 */
//获取热搜列表
@Override
@Transactional
    public HotSearchListResponseDTO getHotSearchList(int currentPage, int pageSize,String keyword){
    //1.参数校验
    if(currentPage<0 || pageSize<=0){
        Result.error("分页参数无效!");
    }
    int start = (currentPage - 1) * pageSize;
    List<HotSearchPostDTO> hotSearches;
    int totalCount;
    int totalPages;
    //根据关键字搜索用户列表实现逻辑
    if(keyword!= null &&!keyword.isEmpty()){
        //执行搜索逻辑
        hotSearches = adminMapper.searchHotSearch(keyword,start,pageSize);
        totalCount = adminMapper.getHotSearchCountByKeyword(keyword);
        totalPages = (int) Math.ceil((double) totalCount / pageSize);
    } else {
        //执行用户正常展示逻辑
        hotSearches = adminMapper.getHotSearchList(start,pageSize);
        totalCount = adminMapper.getHotSearchCount();
        totalPages = (int) Math.ceil((double) totalCount / pageSize);
    }
    //将封装后的数据返回到Controller层
    return new HotSearchListResponseDTO(
            hotSearches,
            new PageInfo(totalCount,pageSize,currentPage),
            totalPages
    );
}

//限流管理
/*@Override
public List<HotSearchPostDTO> getAllActiveHotSearch(){
    return adminMapper.getAllActiveHotSearch();
    }*/

/*
//2.获取热搜总数
@Override
   public int getAllHotSearchCount(){
    return adminMapper.getHotSearchCount();
}
//根据关键词获取热搜总数
@Override
   public int getHotSearchCountByKeyword(String keyword){
    return adminMapper.getHotSearchCountByKeyword(keyword);
}
*/

//根据id获取热搜信息(DTO)
@Override
   public HotSearchPostDTO getHotSearchDTOById(int hotSearchId){
    //1.参数校验
    if(hotSearchId <= 0) {
        Result.error("热搜ID无效!");
    }
    //3.执行方法
    HotSearchPostDTO hotSearchPostDTO = adminMapper.getHotSearchDTOById(hotSearchId);
    if(hotSearchPostDTO == null) {
        Result.error("热搜不存在!");
    }
    return hotSearchPostDTO;
}

//根据id获取热搜信息
    @Override
    public HotSearch getHotSearchById(int hotSearchId){
        //1.参数校验
        if(hotSearchId <= 0) {
            Result.error("热搜ID无效!");
        }
        //3.执行方法
        HotSearch hotSearch = adminMapper.getHotSearchById(hotSearchId);
        if(hotSearch == null) {
            Result.error("热搜不存在!");
        }
        return hotSearch;
    }


//根据id获取新闻信息
@Override
   public Post getPostById(int postId){
    //1.参数校验
    if(postId <= 0) {
        Result.error("新闻id不存在!");
    }
    //2.执行方法
    Post post = adminMapper.getPostById(postId);
      if(post == null) {
          Result.error("新闻不存在");
      }
      return post;
}

/*
//4.新添热搜并初始化状态
@Transactional
@Override
   public int addPost(Post post){
   //1.参数校验
    if(post == null || StringUtils.isEmpty(post.getTitle())){
        Result.error("新闻内容不能为空!");
    }
   //2.插入热搜信息
    adminMapper.addHotSearch(post);
    Result.success(post,"热搜添加成功!");
    return 0;
}
@Override
    public void addHotSearch(HotSearch hotSearch){
    //1.参数校验
    if(hotSearch == null) {
        Result.error("热搜信息不能为空!");
    }
    if (hotSearch.getKeyword() == null) {
        hotSearch.setKeyword(""); // 设置为空字符串或其他默认值
    }
    //2.插入热搜信息
    adminMapper.addHotSearchStatus(hotSearch);
    Result.success(hotSearch,"热搜状态初始化成功!");
}
*/

//5.更新热搜的部分信息
@Override
    public void updateHotSearch(HotSearchPostDTO hotSearch){
   try {
       //1.参数校验
       if (hotSearch != null && hotSearch.getHotSearchId() <= 0) {
           Result.error("热搜ID无效!");
       }
       if (hotSearch == null) {
           Result.error("无该热搜信息!");
       }
       //2.更新热搜信息
       adminMapper.updateHotSearch(hotSearch);
       Result.success(hotSearch, "热搜信息更新成功!");
   } catch (Exception e) {
       Result.error("更新失败");
   }
}

//修改单个热搜状态(是否屏蔽)
@Override
@Transactional
    public void updateHotSearch_Blocked(int hotSearchId,boolean block){
    HotSearch hotSearch = adminMapper.getHotSearchById(hotSearchId);
    //1.参数校验
    if(hotSearchId <= 0){
        Result.error("热搜ID无效!");
    }
    //2.热搜信息校验
    if(hotSearch != null){
        hotSearch.setIsBlocked(block ? 1 : 0);
        adminMapper.updateHotSearchBlocked(hotSearchId,block);
        Result.success("热搜屏蔽成功!");
    }else{
        Result.error("该热搜信息不存在!");
    }
}

//修改单个热搜状态(是否限流)
@Override
@Transactional
   public void updateHotSearch_Limited(int hotSearchId,boolean limit){
    HotSearch hotSearch = adminMapper.getHotSearchById(hotSearchId);
   //1.参数校验
   if(hotSearchId <= 0){
       Result.error("热搜ID无效!");
   }
   //2.热搜信息校验
    if(hotSearch != null){
        hotSearch.setIsLimited(limit ? 1 : 0);
        adminMapper.updateHotSearchLimited(hotSearchId,limit);
        Result.success("热搜限流成功!");
    }else{
        Result.error("该热搜信息不存在!");
    }
}
//6.3 对限流检查,若成功访问,访问数增一
@Override
public boolean canAccessHotSearch(int hotSearchId) {
    // 1. 检查是否被限流
    Boolean isLimited = adminMapper.getHotSearchLimitStatus(hotSearchId);
    if(isLimited != null && isLimited) {
        return false;
    }
    // 2. 更新访问计数
    adminMapper.incrementHotSearchViewCount(hotSearchId);
    return true;
}

//批量修改热搜状态
@Override
    public void updateHotSearchStatusByIds(List<HotSearch> updateList){
   try {
       //1.参数校验
       if (updateList == null || updateList.size() <= 0) {
           Result.error("热搜更新列表不能为空!");
       }
       //2.批量更新热搜状态
       for (HotSearch hotSearch : updateList) {
           if (hotSearch.getHotSearchId() == -1) {
               Result.error("热搜ID不能为空!");
           } else {
               adminMapper.updateHotSearchStatusBatch(updateList);
               Result.success("批量更新热搜状态成功!");
           }
       }
   }catch (Exception e){
        Result.error("批量更新失败!");
       }
}

//删除单个热搜
@Override
    public void deleteHotSearchById(int hotSearchId){
   try {
       //1.参数校验
       if (hotSearchId <= 0) {
           Result.error("热搜ID无效!");
       }
       //2.检查热搜是否存在
       HotSearch hotSearch = adminMapper.getHotSearchById(hotSearchId);
       if (hotSearch == null) {
           Result.error("热搜不存在!");
       } else {
           adminMapper.deleteHotSearchById(hotSearchId);
           Result.success("热搜删除成功;");
       }
   } catch (Exception e) {
       Result.error("删除失败");
   }
}

//批量删除多个热搜
@Override
    public void deleteHotSearchBatch(int []hotSearchIds){
    try{
    //1.参数校验
    if(hotSearchIds.length == 0 ){
        Result.error("热搜ID列表不能为空!");
    }
    //2.检查热搜是否存在
    for(int hotSearchId : hotSearchIds){
        if(hotSearchId <= 0){
            Result.error("热搜ID无效!");
        }
        else{
            adminMapper.deleteHotSearchBatch(hotSearchIds);
            Result.success("批量删除成功!");
        }
    }
    }catch (Exception e){
        Result.error("批量删除失败!");
    }
}

/*
//10.根据关键字查询热搜
@Override
    public List<HotSearchPostDTO> searchHotSearch(String keyword,int start,int pageSize) {
    //1.参数校验
    if (StringUtils.isEmpty(keyword)) {
        Result.error("查询的关键词不能为空!");
    }
    if(start <0 || pageSize <0){
        Result.error("参数有误!");
    }
    //2.查询热搜
    List<HotSearchPostDTO> hotSearches = adminMapper.searchHotSearch(keyword,start,pageSize);
    if (hotSearches.isEmpty()) {
        Result.error("未找到相关热搜!");
    }
    return hotSearches;
}
*/


/* 管理员对标签的管理 */

//1.获取标签列表
@Transactional
@Override
   public List<Tag> getTagList(){
    adminMapper.updatePostNumber();
    return adminMapper.getAllTag();
}

//2.添加标签
@Override
   public void insertTag(Tag tag){
    //1.参数校验
    if(tag == null || StringUtils.isEmpty(tag.getTagName())) {
        Result.error("类别名称不能为空!");
    }
    //2.执行方法
    if (tag != null) {
        adminMapper.insertTag(tag.getTagName(),tag.getTagDescription());
        Result.success(tag,"类别添加成功!");
    } else
    {
        Result.error("类别添加失败!");
    }
}

//3.修改标签状态
@Override
    public void updateTag(Tag tag){
    //1.参数校验
    if(tag == null || StringUtils.isEmpty(tag.getTagName())) {
        Result.error("类别名称不能为空!");
    }
    //2.执行方法
    if (tag != null) {
        adminMapper.updateTag(tag.getTagId(), tag.getTagStatus());
    }
    else {
        Result.error("类别更新失败");
    }
}

//4.删除标签
@Override
    public void deleteTagById(int tagId){
 try {
     //1.参数校验
     if (tagId <= 0) {
         Result.error("类别ID无效!");
     }
     //2.执行方法
     adminMapper.deleteTagById(tagId);
 }catch (Exception e){
     Result.error("删除失败");
 }
}

//5.批量删除标签
@Override
   public void deleteTagByIds(int []tagIds){
  try {
      //1.参数校验
      if (tagIds.length == 0) {
          Result.error("参数无效!");
      }
      //2.执行方法
      for (int tagId : tagIds) {
          if (tagId <= 0) {
              Result.error("类别id无效!");
          }
          adminMapper.deleteTagBatch(tagIds);
      }
  }catch (Exception e){
      Result.error("批量删除失败");
  }
}

//6.查询标签
@Override
    public List<Tag> searchTag(String keyword){
    //1.参数校验
    if(StringUtils.isEmpty(keyword)) {
        Result.error("查询的关键词不能为空!");
    }
    //2.查询标签
    return adminMapper.searchTag(keyword);
}

//7.根据标签id获取标签信息
@Override
    public Tag getTagById(int tagId){
    return adminMapper.getTagByTagId(tagId);
    }

//根据postId获取标签Id
@Override
   public int getTagIdByPostId(int postId){
    return adminMapper.getTagIdByPostId(postId);
}

//编辑标签
@Override
    public void editTagById(Tag tag) {
  try {
      //1.参数校验
      if (tag != null && tag.getTagId() <= 0) {
          Result.error("标签ID无效!");
      }
      if (tag == null) {
          Result.error("无该标签!");
      }
      //2.执行方法
      adminMapper.editTagByTagId(tag);
      Result.success("标签编辑成功!");
  }
  catch (Exception e){
      Result.error("编辑失败!");
  }
}


/* 管理员审核 */

//获取待审核新闻列表(分页查询)
@Override
@Transactional
    public AuditPostResponseDTO getAuditingPostList(int currentPage, int pageSize,String keyword){
    //1.参数校验
    if(currentPage < 0 || pageSize < 0) {
        Result.error("分页参数无效!");
    }
    int start = (currentPage - 1) * pageSize;
    List<AuditPostDTO> auditPostList;
    int totalCount;
    int totalPage;
    //根据关键字搜索用户列表实现逻辑
    if(keyword!= null &&!keyword.isEmpty()){
        //执行搜索逻辑
        auditPostList = adminMapper.searchAuditPost(keyword,start,pageSize);
        totalCount = adminMapper.getAuditPostCountByKeyword(keyword);
        totalPage = (int) Math.ceil((double) totalCount / pageSize);
    } else {
        //执行用户正常展示逻辑
        auditPostList = adminMapper.getAuditPostList(start,pageSize);
        totalCount = adminMapper.getAuditPostCount();
        totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }
    return new AuditPostResponseDTO(
            auditPostList,
            new PageInfo(totalCount,pageSize,currentPage),
            totalPage
    );

}

/*//2.获取待审核新闻总数
@Override
    public int getAllAuditingPostCount(){
    return adminMapper.getAuditPostCount();
}*/
/*
//根据关键词获取待审核新闻
@Override
    public List<AuditPostDTO> searchAuditingPost(String keyword,int start, int pageSize){
    //1.参数校验
    if(StringUtils.isEmpty(keyword)) {
        Result.error("查询的关键词不能为空!");
    }
    //2.执行方法
    return adminMapper.searchAuditPost(keyword);
    }

//根据关键词获取待审核新闻总数
@Override
    public int getAuditingPostCountByKeyword(String keyword){
    //1.参数校验
    if(StringUtils.isEmpty(keyword)) {
        Result.error("查询的关键词不能为空!");
    }
    //2.执行方法
    return adminMapper.getAuditPostCountByKeyword(keyword);
    }
*/


//2.1审核通过
@Override
@Transactional
    public void updatePostAuditing(int postId,User adminUser){
    //1.参数校验
    if(postId <= 0) {
        Result.error("参数Id无效!");
    }
    if(adminUser == null) {
        Result.error("管理员未登录!");
    }
    //获取关联数据
    int tagId = getTagIdByPostId(postId);
    Tag tag = getTagById(tagId);
    if(tag == null) Result.error("标签不存在!");
    //执行审核操作
    adminMapper.updatePostAudit(postId);
    //生成审核记录
    AuditPost auditPost = null;
    if (adminUser != null && tag != null) {
            auditPost = new AuditPost(postId,adminUser.getUserId(),adminUser.getUserName(),tag.getTagName(),tagId,"approved","通过!");
    }
    //插入审核记录
    adminMapper.insertAuditLog(auditPost);
    Result.success("审核通过!");
}

//2.2审核不通过
@Override
@Transactional
    public void updatePostNotAuditing(int postId,User adminUser,String auditReason){
    //1.参数校验
    if(postId <= 0) {
        Result.error("参数Id无效!");
    }
    if(adminUser == null) {
        Result.error("管理员未登录!");
    }
    //获取关联数据
    int tagId = getTagIdByPostId(postId);
    Tag tag = getTagById(tagId);
    if(tag == null) Result.error("标签不存在!");
    //执行审核操作
    adminMapper.updatePostNotAudit(postId);
    //生成审核记录
    AuditPost auditPost = null;
    if (adminUser != null && tag != null) {
        auditPost = new AuditPost(postId,adminUser.getUserId(),adminUser.getUserName(),tag.getTagName(),tagId,"rejected",auditReason);
    }
    //插入审核记录
    adminMapper.insertAuditLog(auditPost);
    Result.success("审核通过!");
}

//3.1批量审核通过
@Override
@Transactional
    public void updatePostAuditingBatch(int[] postIds, User adminUser) {
      //1.参数校验
      if(postIds == null || postIds.length == 0) {
          Result.error("参数无效!");
      }
      if(adminUser == null) {
          Result.error("管理员未登录!");
      }
      //关联数据
    if (postIds != null) {
        for (int postId : postIds) {
            int tagId = getTagIdByPostId(postId);
            Tag tag = getTagById(tagId);
            if (tag == null) {
                Result.error("标签不存在!");
            } else {
            //执行批量审核操作
            adminMapper.updatePostAudit(postId);
            //生成审核记录
                AuditPost auditPost = null;
                if (adminUser != null) {
                    auditPost = new AuditPost(postId, adminUser.getUserId(), adminUser.getUserName(), tag.getTagName(), tagId, "approved", "通过!");
                }
                adminMapper.insertAuditLog(auditPost);
           }
        }
    }
    }

//3.2批量审核不通过
@Override
@Transactional
    public void updatePostNotAuditingBatch(int[] postIds,User adminUser,String auditReason) {
    //1.参数校验
    if(postIds == null || postIds.length == 0) {
        Result.error("参数无效!");
    }
    if(adminUser == null) {
        Result.error("管理员未登录!");
    }
    //关联数据
    if (postIds!= null) {
        for (int postId : postIds) {
            int tagId = getTagIdByPostId(postId);
            Tag tag = getTagById(tagId);
            if (tag == null) {
                Result.error("标签不存在!");
            } else {
              //执行批量审核操作
                adminMapper.updatePostNotAudit(postId);
              //生成审核记录
              AuditPost auditPost = null;
              if (adminUser != null) {
                  auditPost = new AuditPost(postId, adminUser.getUserId(), adminUser.getUserName(), tag.getTagName(), tagId, "rejected", auditReason);
              }
              adminMapper.insertAuditLog(auditPost);
            }
        }
    }
    }

//将审核记录上传至审核记录表中
@Override
public void insertAuditLog(AuditPost auditPost) {
    //1.参数校验
    if(auditPost == null) {
        Result.error("参数无效!");
    }
    //2.执行方法
    adminMapper.insertAuditLog(auditPost);
}


//获取审核记录分页表(分页查询)
@Override
@Transactional
    public AuditPostLogResponseDTO getAuditedPostList(String keyword,int currentPage, int pageSize) {
    //1.参数校验
    if (currentPage < 0 || pageSize < 0) {
        Result.error("参数无效!");
    }
    int start = (currentPage - 1) * pageSize;
    List<AuditPost> auditPost;
    int totalCount;
    int totalPage;
    //根据关键字搜索用户列表实现逻辑
    if (keyword != null && !keyword.isEmpty()) {
        //执行搜索逻辑
        auditPost = adminMapper.getAuditPostByKeyword(keyword, start, pageSize);
        totalCount = adminMapper.getAuditPostLogCountByKeyword(keyword);
        totalPage = (int) Math.ceil((double) totalCount / pageSize);
    } else {
        //执行用户正常展示逻辑
        auditPost = adminMapper.getAuditPosts(start,pageSize);
        totalCount = adminMapper.getAuditAllPosts();
        totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }
    return new AuditPostLogResponseDTO(
            auditPost,
            new PageInfo(totalCount,pageSize,currentPage),
            totalPage
    );
}

//11.根据id获取审核记录
@Override
    public AuditPost getAuditedPostById(int postId) {
       //1.参数校验
    if(postId <= 0){
        Result.error("参数无效!");
    }
      //2.执行方法
    return adminMapper.getAuditPostById(postId);
    }

//12.删除审核记录
@Override
    public void deleteAuditedPostById(int auditId) {
      //1.参数校验
    if(auditId <= 0) {
        Result.error("参数无效!");
    }
      //2.执行方法
    adminMapper.deleteAuditPostById(auditId);
    }

//13.批量删除审核记录
@Override
    public void deleteAuditedPostByIds(int[] auditIds) {
     //1.参数校验
    if(auditIds == null || auditIds.length == 0) {
        Result.error("参数无效!");
    }
    //2.执行方法
    for (int auditId : auditIds) {
        AuditPost auditPost = adminMapper.getAuditPostById(auditId);
        if(auditId <= 0 || auditPost == null) {
            Result.error("参数无效!");
        }else{
            adminMapper.deleteAuditPostById(auditId);
            Result.success("删除成功!");
        }
      }
    }


}





